package com.gitee.wsl.mathematics.geometry.d3.edge

import com.gitee.wsl.mathematics.geometry.d3.vertex.AnyKind
import com.gitee.wsl.mathematics.geometry.d3.face.FaceKind
import com.gitee.wsl.mathematics.geometry.d3.vertex.VertexKind
import com.gitee.wsl.mathematics.geometry.d3.face.toFaceKindOrNull
import com.gitee.wsl.mathematics.geometry.d3.vertex.toVertexKindOrNull
import kotlinx.serialization.Serializable

@Serializable
data class EdgeKind(val a: VertexKind, val b: VertexKind, val l: FaceKind, val r: FaceKind) : AnyKind, Comparable<EdgeKind> {
    override fun compareTo(other: EdgeKind): Int {
        if (a != other.a) return a.compareTo(other.a)
        if (b != other.b) return b.compareTo(other.b)
        if (l != other.l) return l.compareTo(other.l)
        return r.compareTo(other.r)
    }

    override fun toString(): String = "$a-$l/$r-$b"
}

fun String.toEdgeKindOrNull(): EdgeKind? {
    val s = split("-").takeIf { it.size == 3 } ?: return null
    val a = s[0].toVertexKindOrNull() ?: return null
    val b = s[2].toVertexKindOrNull() ?: return null
    val t = s[1].split("/").takeIf { it.size == 2 } ?: return null
    val l = t[0].toFaceKindOrNull() ?: return null
    val r = t[1].toFaceKindOrNull() ?: return null
    return EdgeKind(a, b, l, r)
}

fun EdgeKind.reversed(): EdgeKind = EdgeKind(b, a, r, l)
